package com.ruyuan.engine.core;
import com.ruyuan.engine.datagen.MockDataUtils;
import com.ruyuan.engine.datagen.RuleComplexParamDataGen;
import com.ruyuan.engine.pojo.ClientLog;
import com.ruyuan.engine.pojo.RuleComplexParam;
import com.ruyuan.engine.pojo.RuleResult;
import com.ruyuan.engine.service.*;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 规则核心处理逻辑
 * */
public class RuleEngineProcessStatic extends KeyedProcessFunction<String, ClientLog, RuleResult> {

    protected static final Logger log = LoggerFactory.getLogger(RuleEngineProcessStatic.class);

    /**
     * 注入服务
     * */
    private BaseRuleService baseRuleService;
    private CombinationRuleService combinationRuleService;
    private SequenceRuleService sequenceRuleService;

    ListState<ClientLog> clientLogListState;

    //动态生成的规则对象
    RuleComplexParam ruleComplexParam;

    @Override
    public void open(Configuration parameters) throws Exception {
        clientLogListState = getRuntimeContext().getListState(new ListStateDescriptor<ClientLog>("clientLogListState",ClientLog.class));
        baseRuleService = new BaseRuleServiceImpl();
        combinationRuleService = new CombinationRuleServiceImpl(clientLogListState);
        sequenceRuleService = new SequenceRuleServiceImpl(clientLogListState);
        ruleComplexParam = RuleComplexParamDataGen.mockData();
    }

    @Override
    public void processElement(ClientLog clientLog, Context ctx, Collector<RuleResult> out) throws Exception {
        clientLogListState.add(clientLog);
        //判断是否满足规则出发条件
        if (ruleComplexParam.getTrigger().getEventId().equals(clientLog.getEventId())) {
            //基本的规则
            boolean condition1 = baseRuleService.mathRuleByCondition(clientLog, ruleComplexParam);

            //组合规则
            boolean condition2 = combinationRuleService.mathRuleConditionCount(clientLog.getEventId(), ruleComplexParam);

            //顺序组合规则
            boolean condition3 = sequenceRuleService.mathRuleSequenceCount(clientLogListState, ruleComplexParam);
            if (condition1 || condition2 || condition3) {
                RuleResult result1 = new RuleResult();
                result1.setCreateTime(clientLog.getDetailTime());
                result1.setRuleId(MockDataUtils.ruleIdGen());
                result1.setEventId(clientLog.getEventId());
                result1.setRuleComplexParam(ruleComplexParam);
                if (condition1) result1.setIsBaseRuleParams(condition1);
                if (condition2) result1.setIsBaseRuleParams(condition2);
                if (condition3) result1.setIsBaseRuleParams(condition3);
                out.collect(result1);
                log.info("基本的规则结果为：{} 组合规则结果为：{} 顺序组合规则结果为：{}",condition1,condition2,condition3);
            }else {
                log.info("不匹配规则{} ",clientLog.getUserNo());
            }
        }
    }
}